package com.sky.controller.admin;

import cn.dev33.satoken.stp.StpUtil;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.entity.Employee;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @PostMapping("login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO){
        log.info("接收到的参数：{}",employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //签发token
        StpUtil.login(employee.getId());
        String token = StpUtil.getTokenValue();
        log.info("token={}",token);

        EmployeeLoginVO employeeLoginVO=new EmployeeLoginVO();
        employeeLoginVO.setId(employee.getId());
        employeeLoginVO.setName(employee.getName());
        employeeLoginVO.setUserName(employee.getUsername());
        employeeLoginVO.setToken(token);
        return Result.success(employeeLoginVO);
    }

    @PostMapping("")
    public Result<Object> add(@RequestBody EmployeeDTO employeeDTO){
        //todo 完成对dto的检查
        employeeService.add(employeeDTO);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result<Employee> findById(@PathVariable long id){
       Employee employee= employeeService.findById(id);
       return Result.success(employee);
    }

    
    @GetMapping("page")
    public Result<PageResult> page(int page, int pageSize ){
        log.info("page:{},pageSize:{}",page,pageSize);
        PageResult pageResult=  employeeService.getPage(page,pageSize);
        return Result.success(pageResult);
    }

    @PutMapping
    public Result<Object> update(@RequestBody EmployeeDTO employeeDTO){
        employeeService.update(employeeDTO);
        return Result.success();
    }

    @PostMapping("status/{status}")
    public Result<Object> status(@PathVariable int status,long id ){
        Employee employee=new Employee();
        employee.setStatus(status);
        employee.setId(id);
        employeeService.status(employee);
        return Result.success();
    }

}
